﻿using Libs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEngine;

namespace HotFix_Project
{
    class TestProtobuf
    {
        public static void RunTest()
        {
            BaseProto proto = new BaseProto();
            proto.cid = 100;
            proto.nnRoomInfos.Add(new BaseProto2() { roomID = 10 });
            proto.nnRoomInfos.Add(new BaseProto2() { roomID = 11 });
            proto.nnRoomInfos.Add(new BaseProto2() { roomID = 12 });
            proto.nnRoomInfos.Add(new BaseProto2() { roomID = 13 });
            proto.nnRoomInfos.Add(new BaseProto2() { roomID = 14 });

            #region MyRegion
            FileStream fs = new FileStream(@Application.persistentDataPath + "/output.txt", FileMode.OpenOrCreate);
            ProtoBuf.Serializer.Serialize<BaseProto>(fs, proto);
            fs.Close();
            Debug.Log("[Hotfix]:序列化完成,path:" + @Application.persistentDataPath + "/output.txt");

            Debug.Log("[Hotfix]:读取序列化二进制并反序列化");
            byte[] data = File.ReadAllBytes(@Application.persistentDataPath + "/output.txt");
            MemoryStream ms = new MemoryStream(data);
            BaseProto baseproto = ProtoBuf.Serializer.Deserialize<BaseProto>(ms);
            Debug.Log("[Hotfix]:cid:" + baseproto.cid + " room count:" + baseproto.nnRoomInfos.Count);

            for (int i = 0; i < baseproto.nnRoomInfos.Count; i++)
            {
                Debug.Log("[Hotfix]:roomId:" + baseproto.nnRoomInfos[i].roomID);
            }
            #endregion

            proto.nnRoomInfos2 = new BaseProto2[2];
            proto.nnRoomInfos2[0] = new BaseProto2() { roomID = 1001 };
            proto.nnRoomInfos2[1] = new BaseProto2() { roomID = 1002 };

            System.IO.MemoryStream stream = new System.IO.MemoryStream();

            ProtoBuf.Serializer.Serialize(stream, typeof(BaseProto), proto);


            byte[] bytes = stream.ToArray();
            stream.Dispose();

            UnityEngine.Debug.Log("[Hotfix]:bytes length:"+bytes.Length);

            stream = new System.IO.MemoryStream(bytes);
            var obj = ProtoBuf.Serializer.Deserialize(typeof(BaseProto), stream) as BaseProto;

            UnityEngine.Debug.Log("[Hotfix]:" + obj.nnRoomInfos2[0].roomID + " - " + obj.nnRoomInfos2[1].roomID);
        }
    }
}
